package com.istock.union.controller.security;

import java.util.HashSet;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.istock.base.common.api.model.ApiBaseResponse;
import com.istock.base.common.api.model.PageInfo;
import com.istock.base.common.api.model.RetStatusEnum;
import com.istock.base.export.util.DataTranslateUtils;
import com.istock.union.user.client.SSOClientUtils;
import com.istock.union.user.model.Constants;
import com.istock.union.user.model.DeptInfo;
import com.istock.union.user.model.SSOUser;
import com.istock.union.user.model.UserCredentialInfo;
import com.istock.union.user.model.UserCredentialVO;
import com.istock.union.user.model.UserDept;
import com.istock.union.user.security.InnerTokenService;
import com.istock.union.user.service.DeptInfoService;
import com.istock.union.user.service.UserCredentialInfoService;
import com.istock.union.user.translate.UserIdTranslateCallback;

@Controller
@RequestMapping("/s/afterLogin")
public class ChooseDeptController {

	@Autowired
	private UserCredentialInfoService credentialInfoService;
	
	@Autowired
	private DeptInfoService deptInfoService;
	@Autowired
	private InnerTokenService tokenService;
	
	@RequestMapping("showDept")
	public String showDept() {
		return "afterLogin/chooseDept";
	}
	
	@RequestMapping("search")
	@ResponseBody
	public ApiBaseResponse<PageInfo<List>> search(){
		SSOUser ssoUser = SSOClientUtils.getInstance().findCurrentUser();
		List<UserCredentialVO> credentialList = credentialInfoService.findUserCredentialByUserId(ssoUser.getUserId());
		List result = DataTranslateUtils.translate(credentialList, "userId", "userInfo", new UserIdTranslateCallback());
		PageInfo<List> pageInfo = new PageInfo<List>();
		pageInfo.setData(result);
		pageInfo.setTotalPage(10);
		pageInfo.setTotalRecord(result.size());
		return new ApiBaseResponse<PageInfo<List>>(RetStatusEnum.SUCCESS,Constants.API_RESPONSE_SUCCESS_CODE,  "查询成功" , pageInfo);
	}
	
	@RequestMapping("chooseDept")
	@ResponseBody
	public ApiBaseResponse<Boolean> chooseDept(String id){
		UserCredentialInfo credentialInfo = credentialInfoService.findCredentialById(id);
		if(credentialInfo != null) {
			SSOUser ssoUser = SSOClientUtils.getInstance().findCurrentUser();
			
			DeptInfo deptInfo = deptInfoService.selectDeptByCode(credentialInfo.getDeptCode());
			if(deptInfo != null) {
				UserDept userDept = new UserDept();
				userDept.setAncestor(deptInfo.getAncestors());
				userDept.setDeptCode(deptInfo.getDeptCode());
				userDept.setDeptName(deptInfo.getDeptName());
				userDept.setId(deptInfo.getId());
				userDept.setTenantCode(deptInfo.getTenantCode());
				ssoUser.setCurrentDept(userDept);
				
				ssoUser.setCredentialId(id);
				
				tokenService.saveTokenModel(ssoUser);
			}
		}
		return new ApiBaseResponse<Boolean>(RetStatusEnum.SUCCESS ,Constants.API_RESPONSE_SUCCESS_CODE , "设置成功",true );
	}
	
	@RequestMapping("cancelDept")
	@ResponseBody
	public ApiBaseResponse<Boolean> cancelDept(){
		SSOUser ssoUser = SSOClientUtils.getInstance().findCurrentUser();
		ssoUser.setPermissions(new HashSet<String>());
		ssoUser.setCurrentDept(null);
		ssoUser.setCredentialId(null);
		tokenService.saveTokenModel(ssoUser);
		return new ApiBaseResponse<Boolean>(RetStatusEnum.SUCCESS ,Constants.API_RESPONSE_SUCCESS_CODE , "设置成功",true );
	}
}
